'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 定时任务
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var fightPowerService = require('../../services/fightPowerService.js');
var playerManager = require('../../libs/playerManager');
var roleModel = require('../../models/roleModel.js');

module.exports = function (cb) {
    var refresh_time = moment().add(-1, 'days');
    roleModel.findRoles({
        last_login_time: {
            $gt: refresh_time
        }
    }, function (err, role_list) {
        async.forEach(role_list, function (role_item, cb) {
                _updateFightPower(role_item.role_id, cb);
            },
            function (err) {
                if (typeof (cb) == 'function') {
                    cb();
                }
            }
        );
    });
};

var _updateFightPower = function (role_id, cb) {
    var player_manager = new playerManager(role_id);
    var model_list = ['role', 'skill', 'build', 'soldier', 'tech'];
    player_manager.getModel(model_list, function (err) {
        fightPowerService.updateMainPower(player_manager);
        player_manager.save();
        cb();
    });
};
